package com.a2.appa3.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Fragment基类
 * 构造基本逻辑与添加常用方法
 * 未绑定presenter
 */
public abstract class BaseFragmentSimple extends Fragment {
    private Unbinder unbinder;
    private int position;
    private String title;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(setContentView(), container, false);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        //TODO 添加功能
    }

    public void setPosition(int position) {
        this.position = position;
    }

    @Override
    public void onPause() {
        super.onPause();
        //TODO 移除功能
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * 关闭软键盘
     *
     * @param view getWindowToken()
     */
    protected void closeKeyboard(@NonNull View view) {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     * @return 默认SharedPreferences
     */
    protected SharedPreferences getSharedPreferences() {
        return getActivity().getSharedPreferences("", Activity.MODE_PRIVATE);
    }

    /**
     * 弹吐司
     *
     * @param stringRes 文字资源
     */
    protected void showToast(@StringRes int stringRes) {
        Toast.makeText(getContext(), getString(stringRes), Toast.LENGTH_SHORT).show();
    }

    /**
     * 设置contentView
     *
     * @return 布局id
     */
    protected abstract int setContentView();

    /**
     * 显示提示信息
     */
    protected void showDialog(@StringRes int title,
                           @StringRes int msg,
                           @StringRes int negative,
                           DialogInterface.OnClickListener negativeListener,
                           @StringRes int positive,
                           DialogInterface.OnClickListener positiveListener,
                           boolean cancelable) {
        new AlertDialog.Builder(getContext())
                .setTitle(title)
                .setMessage(msg)
//                .setNegativeButton(negative, negativeListener)
//                .setPositiveButton(positive, positiveListener)
                .setPositiveButton(negative, negativeListener)
                .setNegativeButton(positive, positiveListener)
                .setCancelable(cancelable)
                .show();
    }

}
